package cn.mengxw.evcard.entity.cmd;

import cn.mengxw.evcard.config.ConfigManage;
import cn.mengxw.evcard.entity.KeywordReply;
import org.apache.commons.lang3.StringUtils;
import org.meowy.cqp.jcq.entity.CoolQ;
import org.meowy.cqp.jcq.entity.Group;

import java.util.List;
import java.util.Map;

public class AddAutoReply extends BaseCommand {
  public AddAutoReply(CoolQ CQ, Command command, Long fromQQ) {
    super(CQ, command, fromQQ);
  }

  @Override
  public void run() {
    List<Long> groupIds = command.getList();
    if (StringUtils.equals(command.getRange(), "part")
        && groupIds != null
        && groupIds.size() != 0) {
      groupIds.forEach(
          g -> {
            // 更新回复列表
            Map<String, KeywordReply> replys = ConfigManage.getKeywordReplysByGroupId(g);
            KeywordReply keywordReply = new KeywordReply();
            keywordReply.setGroup(g);
            keywordReply.setReplyContent(command.getMsg());
            keywordReply.setKeyword(command.getKey());
            keywordReply.setEnableTime(command.getEnableTime());
            keywordReply.setOffTime(command.getOffTime());
            replys.put(command.getKey(), keywordReply);
          });
      // 指定了群列表

    } else if (StringUtils.equals(command.getRange(), "all")) {

      CQ.getGroupList().stream()
          .map(Group::getId)
          .map(ConfigManage::getKeywordReplysByGroupId)
          .forEach(
              map -> {
                // 更新回复列表
                KeywordReply keywordReply = new KeywordReply();
                keywordReply.setReplyContent(command.getMsg());
                keywordReply.setKeyword(command.getKey());
                keywordReply.setEnableTime(command.getEnableTime());
                keywordReply.setOffTime(command.getOffTime());
                map.put(command.getKey(), keywordReply);
              });
    } else {

      ConfigManage.getenableRobotSet().stream()
          .forEach(
              g -> {
                // 更新回复列表
                Map<String, KeywordReply> replys = ConfigManage.getKeywordReplysByGroupId(g);
                KeywordReply keywordReply = new KeywordReply();
                keywordReply.setGroup(g);
                keywordReply.setReplyContent(command.getMsg());
                keywordReply.setKeyword(command.getKey());
                keywordReply.setEnableTime(command.getEnableTime());
                keywordReply.setOffTime(command.getOffTime());
                replys.put(command.getKey(), keywordReply);
              });
    }

    CQ.sendPrivateMsg(fromQQ, "自动回复指令已经添加！");
  }
}
